package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsStorage;
import com.qf.service.StorageService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/20 11:44
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/storage")
public class StorageController {
//    page=1&limit=20&sort=add_time&order=desc
    @Autowired
    private StorageService storageService;

    /**
     * 查询对象存储数据列表
     * @param key    根据文件的key进行查找
     * @param name   根据文件名字查找
     * @param page   当前页, 默认值1
     * @param limit  每页显示条数, 默认值10
     * @param sort   排序字段名, 默认按照add_time添加时间排序
     * @param order  排序方式, 默认desc降序
     * @return
     */
    @GetMapping("/list")
    public Object list(String key, String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer limit,
                       @RequestParam(defaultValue = "add_time")String sort,
                       @RequestParam(defaultValue = "desc")String order) {
        //1. 查询并返回分页对象
        IPage<DtsStorage> dtsStorageIPage = storageService.queryStoragePage(key, name, page, limit, sort, order);
        System.out.println("dtsStorageIPage========"+dtsStorageIPage);
        //获取结果集
        List<DtsStorage> records = dtsStorageIPage.getRecords();
        System.out.println("=====records====="+records);
        //获取查询总条数
        long total = dtsStorageIPage.getTotal();

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", total);
        hashMap.put("items",records);
        //封装对象返回
        return ResponseUtil.ok(hashMap);
    }
    @PostMapping("/create")
    public  Object add(@RequestParam("file") MultipartFile file) throws Exception {
        //获取文件名字
        String name = file.getOriginalFilename();
        if(StringUtils.isEmpty(name)){
        throw  new RuntimeException("文件为空");
        }
        //获取文件大小
        long size = file.getSize();
        //获取后缀名
        String substring = name.substring(name.lastIndexOf("."));
        //获取文件内容
        byte[] bytes = file.getBytes();
        //调用service层
        DtsStorage storage = storageService.add(name,(int)size, substring, bytes);
        return ResponseUtil.ok(storage);
    }
    @PostMapping("/update")
    public  Object update (@RequestBody DtsStorage dtsStorage){
        System.out.println("====================="+dtsStorage+"dtsStorage");
        Integer update = storageService.update(dtsStorage);
        if(update==0){
            return  ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage dtsStorage){
        //获取对象的key 也就是 阿里云中图片中的地址
        String key = dtsStorage.getKey();
        storageService.delete(key);
        return ResponseUtil.ok();
    }
}
